package com.sgb.kjwl.viewmodel.packers;

import android.app.Application;
import android.text.TextUtils;

import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.EmploymentEntity;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.link.entity.PickFilterEntity;
import com.sgb.net.entity.ListResponse;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.PickSelectEntity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
*@author liwei
*@date 2020/11/10
*description:
*/
public class PackersEmploymentModel extends AndroidViewModel {


    private int page = 1;
    private int limit = 10;
    public MutableLiveData<Boolean> loading = new MutableLiveData<>();
    public MutableLiveData<List<EmploymentEntity>> employmentList = new MutableLiveData<>();
    public MutableLiveData<List<PickFilterEntity<List<PickFilterEntity<List<PickFilterEntity>>>>>> workType = new MutableLiveData<>();
    public MutableLiveData<List<PickSelectEntity>> employmentStatus = new MutableLiveData<>();
    public MutableLiveData<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> city = new MutableLiveData<>();

    public PackersEmploymentModel(Application application) {
        super(application);
        PickSelectEntity all = new PickSelectEntity();
        all.setNo("");
        all.setProjectscale("全部");
    }

    /**
     * 灵活用工列表
     * @param industryNo
     * @param categoryNo
     * @param workType
     * @param employmentType
     * @param employmentStatus
     * @param provinceNo
     * @param cityNo
     * @param areaNo
     * @param isLoadMore
     */
    public void getEmploymentList(String salaryType,String industryNo, String categoryNo, String workType, String employmentType, String employmentStatus, String provinceNo, String cityNo, String areaNo, boolean isLoadMore) {
        Map<String,Object> map = new HashMap<>();
        page = isLoadMore ? page + 1 : 1;
        map.put("limit", limit);
        map.put("page", page);
        if (!TextUtils.isEmpty(industryNo)) {
            map.put("industryNo", industryNo);
        }
        if (!TextUtils.isEmpty(categoryNo)) {
            map.put("categoryNo", categoryNo);
        }
        if (!TextUtils.isEmpty(workType)) {
            map.put("workType", workType);
        }
        if (!TextUtils.isEmpty(employmentType)) {
            map.put("employmentType", employmentType);
        }
        if (!TextUtils.isEmpty(employmentStatus)) {
            map.put("employmentStatus", employmentStatus);
        }

        if (!TextUtils.isEmpty(salaryType)){
            map.put("salaryType", salaryType);
        }

        if (!TextUtils.isEmpty(provinceNo)) {
            map.put("provinceNo", provinceNo);
        }
        if (!TextUtils.isEmpty(cityNo)) {
            map.put("cityNo", cityNo);
        }
        if (!TextUtils.isEmpty(areaNo)) {
            List<String> stringList = new ArrayList<>();
            stringList.add(areaNo);
            map.put("areaNoList", stringList);
        }
        loading.postValue(true);
        NewAPIManager.getInstance().getTeamNewInterfaceAPI().getEmploymentList(map).enqueue(new ICallBack<BaseEntity<ListResponse<List<EmploymentEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<EmploymentEntity>>> data) {
                DialogHelper.dismissProgressDialog();
                employmentList.postValue(data.getData().getList());
            }
        });
    }

    /**
     * 服务团队-联动工种
     */
    public void getLinkAgeWorkType() {
        loading.postValue(true);
        NewAPIManager.getInstance().getTeamNewInterfaceAPI().getLinkAgeWorkType().enqueue(new ICallBack<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> data) {
                DialogHelper.dismissProgressDialog();
                List<PickFilterEntity<List<PickFilterEntity<List<PickFilterEntity>>>>> data1 = new ArrayList<>();
                for (AddressEntity<List<AddressEntity<List<AddressEntity>>>> datum : data.getData()) {
                    List<PickFilterEntity<List<PickFilterEntity>>> dataSecond = new ArrayList<>();
                    for (AddressEntity<List<AddressEntity>> child : datum.getChildren()) {
                        List<PickFilterEntity> dataThird = new ArrayList<>();
                        for (AddressEntity childChild : child.getChildren()) {
                            dataThird.add(new PickFilterEntity(childChild.getNo(),childChild.getName()));
                        }
                        dataSecond.add(new PickFilterEntity<>(child.getNo(),child.getName(),dataThird));
                    }
                    data1.add(new PickFilterEntity<>(datum.getNo(),datum.getName(),dataSecond));
                }
                PickFilterEntity entity2 = new PickFilterEntity();
                entity2.setNo("");
                entity2.setName("全部");
                data1.add(0,entity2);
                workType.postValue(data1);
            }
        });
    }

    /**
     * 用工状态
     */
    public void getEmploymentStatus() {
        loading.postValue(true);
        NewAPIManager.getInstance().getTeamNewInterfaceAPI().getEmploymentStatus().enqueue(new ICallBack<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<PickSelectEntity>> data) {
                DialogHelper.dismissProgressDialog();
                List<PickSelectEntity> data1 = data.getData();
                if (data1 == null || data1.size() == 0) {
                    data1 = new ArrayList<>();
                }
                employmentStatus.postValue(data1);
            }
        });
    }

    public void getCityList() {
        loading.postValue(true);
        APIManager.getInstance().getPackerAPI().getCityList2().enqueue(new ICallBack<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> data) {
                DialogHelper.dismissProgressDialog();
                city.postValue(data.getData());
            }
        });
    }
}
